//
//  SLHisBoatsViewController.m
//  StarLight
//
//  Created by Duke.wu on 16/5/19.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLHisBoatsViewController.h"
#import "SLNearlyBoatDetailViewModel.h"
#import "SLBoatDetailViewController.h"
#import "SLNearlyCollectionViewCell.h"
#import "SLMapViewController.h"
#import "SLFriendManager.h"
#import "SLFriendAddViewController.h"
#import "STPopup.h"
#import "SLShipOwnerVipLevelView.h"


@interface SLHisBoatsViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,SLNearlyCollectionViewCellDelegate>

@property (nonatomic, strong) SLNearlyBoatDetailViewModel *boatsVM;

@property (nonatomic, strong) NSString *hid;

@property (nonatomic, strong) UICollectionView *boatsCollectionView;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic ,strong) UILabel *nameLB;

@property (nonatomic ,strong) UIImageView *boatsOwnerIcon;

@property (nonatomic, strong) UILabel *typeLabel;

@property (nonatomic, strong) UIButton *buddyBtn;
@property (nonatomic, strong) UIButton *tipBtn;

@property (nonatomic, strong) SLFriendManager *friendMg;
@property (nonatomic, strong) UILabel *vipLabel;

@property (nonatomic, strong) SLShipOwnerVipLevelView *levelView;

@end

@implementation SLHisBoatsViewController



- (void)viewDidLoad {
    [super viewDidLoad];
  [self setUpUI];
  [self addConstrains];
  [self addObservers];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
  self.title = @"他的船舶";
}

#pragma mark - Private Methods

- (void)addObservers{
  @weakify(self)
  
  [[RACObserve(self.boatsVM, moreModel) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(SLBoatMoreModel *model) {
    @strongify(self)
    [self changeStatusWithState:model.buddyState];
    self.nameLB.text = model.name;
    [self.boatsOwnerIcon sd_setImageWithURL:[NSURL URLWithString:model.headPhoto]
                           placeholderImage:[UIImage imageNamed:@"head"]];
    [self setUpVip:model.role];
    [self.boatsCollectionView reloadData];
  }];
  
  [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kAddFriendSuccess object:nil] subscribeNext:^(id x) {
    @strongify(self)
    [self restart];
  }];
}


// 显示操作按钮
static BOOL rightItemSelected = YES;
- (void)showOper{
  rightItemSelected = !rightItemSelected;
  self.tipBtn.hidden = rightItemSelected;
  self.buddyBtn.hidden = rightItemSelected;
}



#pragma mark - Public Methods
- (void)setUpVip:(NSInteger)role{
  if (role == 0) {
    [self.levelView removeFromSuperview];
    self.vipLabel.text = @"普通用户";
    self.vipLabel.textColor = [UIColor blackColor];
  }else if(role > 0){
    [self.levelView configWithLevel:[NSString stringWithFormat:@"%ld",(long)role]];
  }
}


- (void)getMoreBoatWithHid:(NSString *)newHid{
  self.hid = newHid;
  self.boatsVM = [[SLNearlyBoatDetailViewModel alloc] initWithHid:newHid];
}

- (void)changeStatusWithState:(NSInteger)state{
  self.buddyBtn.enabled = YES;
  
  if (state != 3) {
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"操作" style:UIBarButtonItemStylePlain target:self action:@selector(showOper)];
    self.navigationItem.rightBarButtonItem = rightItem;
  };
  
  
  switch (state) {
    case -1:{
      [self.buddyBtn setTitle:@"取消屏蔽" forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = SLBlackColor;
    }
      break;
    case 0:{
      [self.buddyBtn setTitle:@"  添加  " forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = SLBlackColor;
      
    }
      break;
    case 1:{
      [self.buddyBtn setTitle:@" 已添加 " forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = [UIColor grayColor];
      self.buddyBtn.enabled = NO;
    }
      break;
    case 2:{
      [self.buddyBtn setTitle:@"  接受  " forState:UIControlStateNormal];
      self.buddyBtn.backgroundColor = SLBlackColor;
    }
      break;
    case 3:{
      [self.buddyBtn removeFromSuperview];
      [self.tipBtn removeFromSuperview];
    }
      break;
    case 4:{
      [self.buddyBtn removeFromSuperview];
    }
      break;
    default:
      break;
  }
  
}


- (void)didTapedoperFriendBtn{
  @weakify(self)
  SLBoatMoreModel *model = self.boatsVM.moreModel;
  

  
  switch (model.buddyState) {
    case -1:{
      // 取消屏蔽
      [self.friendMg operFriendWithHuid:model.shipOwnerId opers:@"3" block:^{
        @strongify(self)
        [self restart];
      }];
    }
      break;
    case 0:{
      // 添加
      SLFriendAddViewController *addFVC = [SLFriendAddViewController new];
      addFVC.boatModelModel = model;
      STPopupController *popVC = [[STPopupController alloc] initWithRootViewController:addFVC];
      popVC.navigationBarHidden = YES;
      popVC.cornerRadius = 5;
      [popVC presentInViewController:self];
    }
      break;
    case 2:{
      // 接受
      [self.friendMg operFriendWithHuid:model.shipOwnerId opers:@"1" block:^{
        @strongify(self)
        [self restart];
      }];
    }
      break;      
    default:
      break;
  }
  
  
}

- (void)restart{
  [self.boatsVM refreshWithHid:self.hid];
}

- (void)screenFriend{
  @weakify(self)
  SLBoatMoreModel *model = self.boatsVM.moreModel;
  
  [self.friendMg operFriendWithHuid:model.shipOwnerId opers:@"2" block:^{
    @strongify(self)
    [self restart];
  }];
  
}

#pragma mark - UI

- (void)addConstrains{
  
  [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.mas_topLayoutGuideBottom).offset(10);
    make.width.equalTo(self.view).offset(-20);
    make.centerX.equalTo(self.view);
    make.height.equalTo(@(70*MIX_H));
  }];
  
  [self.boatsOwnerIcon mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.headerView).offset(18);
    make.centerY.equalTo(self.headerView);
    make.size.mas_equalTo(CGSizeMake(40*MIX_W, 40*MIX_W));
  }];
  
  [self.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.boatsOwnerIcon.mas_right).offset(15);
    make.centerY.equalTo(self.boatsOwnerIcon);
  }];
  
  [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.nameLB.mas_right).offset(15);
    make.centerY.equalTo(self.boatsOwnerIcon);
  }];
  
  [self.vipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.typeLabel.mas_right).offset(4);
    make.top.equalTo(self.typeLabel);
  }];
  
  [self.levelView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.vipLabel);
    make.left.equalTo(self.vipLabel.mas_right).offset(5);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  
  [self.tipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view);
    make.top.equalTo(self.mas_topLayoutGuideBottom);
    make.size.mas_equalTo(CGSizeMake(50*MIX_W, 30*MIX_W));
  }];
  
  [self.buddyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.size.right.equalTo(self.tipBtn);
    make.top.equalTo(self.tipBtn.mas_bottom);
  }];


  [self.boatsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.width.equalTo(self.view);
    make.bottom.equalTo(self.view);
    make.top.equalTo(self.headerView.mas_bottom).offset(10);
  }];
}

- (void)setUpUI{
  [self.view addSubview:self.headerView];
  [self.view addSubview:self.boatsCollectionView];
  [self.headerView addSubview:self.boatsOwnerIcon];
  [self.headerView addSubview:self.nameLB];
  [self.headerView addSubview:self.typeLabel];
  [self.headerView addSubview:self.vipLabel];
  [self.headerView addSubview:self.levelView];
  [self.view addSubview:self.buddyBtn];
  [self.view addSubview:self.tipBtn];
  
  
}


- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  return [[NSAttributedString alloc] initWithString:@"暂无船舶" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}

#pragma mark - SLNearlyCollectionViewCellDelegate

- (void)currentBtnWasTapedWithShipModel:(SLNearLyShip *)shipModel{
  SLMapViewController *mapVC = [SLMapViewController new];
  [mapVC locationWithLon:shipModel.shiplon lat:shipModel.shiplat];
  [self.navigationController pushViewController:mapVC animated:YES];
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  return self.boatsVM.moreModel.boats.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  SLNearlyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
  [cell configWithType:NearlyCellTypeProduct];
  cell.delegate = self;
  return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(SLNearlyCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
  [cell configNearlyCellWithShipModel:self.boatsVM.moreModel.boats[indexPath.row]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  SLNearLyShip *model = self.boatsVM.moreModel.boats[indexPath.row];
  SLBoatDetailViewController *boatsDetailVC = [SLBoatDetailViewController new];
  boatsDetailVC.isNextAvailable = NO;
  [boatsDetailVC changeBid:model.bid];
  [self.navigationController pushViewController:boatsDetailVC animated:YES];
}




- (UICollectionView *)boatsCollectionView{
  if (!_boatsCollectionView) {
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH-20, 140);
    _boatsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _boatsCollectionView.backgroundColor = [UIColor defeutBackgroudColor];
    [_boatsCollectionView registerClass:[SLNearlyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _boatsCollectionView.delegate = self;
    _boatsCollectionView.dataSource = self;
    _boatsCollectionView.emptyDataSetSource = self;
  }
  return _boatsCollectionView;
}

- (UILabel *)nameLB{
  if (!_nameLB) {
    _nameLB = [UILabel labelwithBoldFontSize:18 textColor:SLBlackColor text:@""];
  }
  return _nameLB;
}

- (UILabel *)typeLabel{
  if (!_typeLabel) {
    _typeLabel = [UILabel labelWithFontSize:16 textColor:SLInkColor text:@"船主"];
  }
  return _typeLabel;
}

- (UIImageView *)boatsOwnerIcon{
  if (!_boatsOwnerIcon) {
    _boatsOwnerIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head"]];
    _boatsOwnerIcon.backgroundColor = SLYellowColor;
  }
  return _boatsOwnerIcon
  
  ;
}

- (UIView *)headerView{
  if (!_headerView) {
    _headerView = [UIView new];
    _headerView.backgroundColor = [UIColor whiteColor];
    _headerView.layer.cornerRadius = 5;
  }
  return _headerView;
}

- (UIButton *)buddyBtn{
  if (!_buddyBtn) {
    _buddyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _buddyBtn.backgroundColor = SLBlackColor;
    [_buddyBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    _buddyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_buddyBtn addTarget:self action:@selector(didTapedoperFriendBtn) forControlEvents:UIControlEventTouchUpInside];
    _buddyBtn.hidden = YES;
  }
  return _buddyBtn;
}

- (UIButton *)tipBtn{
  if (!_tipBtn) {
    _tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_tipBtn setTitle:@" 屏蔽 " forState: UIControlStateNormal];
    [_tipBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _tipBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_tipBtn addTarget:self action:@selector(screenFriend) forControlEvents:UIControlEventTouchUpInside];
    _tipBtn.backgroundColor = SLBlackColor;
    [_tipBtn setTitleColor:SLYellowColor forState:UIControlStateNormal];
    _tipBtn.hidden = YES;
  }
  return _tipBtn;
}


- (SLFriendManager *)friendMg{
  if (!_friendMg) {
    _friendMg = [SLFriendManager new];
  }
  return _friendMg;
}




-(UILabel *)vipLabel{
  if (!_vipLabel) {
    _vipLabel = [UILabel labelwithBoldFontSize:15 textColor:[UIColor redColor] text:@"VIP"];
  }
  return _vipLabel;
}


- (SLShipOwnerVipLevelView *)levelView{
  if (!_levelView) {
    _levelView = [SLShipOwnerVipLevelView new];
  }
  return _levelView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
